﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.Entities;

namespace LibrarySystem.LibraryUtils.DAL.DAOs
{
    public class Library_Receipts : Common
    {

        public int GetDataCount(int skippingID = 0, int employerID = 0)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_Receipt> queryReceipt = _context.Library_Receipts;
            //Filtering
            if (skippingID > 0)
            {
                queryReceipt = queryReceipt.Where(rc => rc.Library_Skipping.Id == skippingID);
            }
            if (employerID > 0)
            {
                queryReceipt = queryReceipt.Where(rc => rc.Library_Employee.Id == employerID);
            }

            return queryReceipt.Count();
        }

        public List<ReceiptModel> GetDataList(int startIndex = 0, int pageSize = 0, string sorting = null,
              int skippingID = 0, int employerID = 0)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_Receipt> queryReceipt = _context.Library_Receipts;
            //Filtering
            if (skippingID > 0)
            {
                queryReceipt = queryReceipt.Where(rc => rc.Library_Skipping.Id == skippingID);
            }
            if (employerID > 0)
            {
                queryReceipt = queryReceipt.Where(rc => rc.Library_Employee.Id == employerID);
            }
            //End Filtering
            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("SkippingID ASC"))
            {
                queryReceipt = queryReceipt.OrderBy(rc => rc.SkippingId);
            }
            else if (sorting.Equals("SkippingID DESC"))
            {
                queryReceipt = queryReceipt.OrderByDescending(rc => rc.SkippingId);
            }
            //
            List<Library_Receipt> receipts = pageSize > 0 ?
                                            queryReceipt.Skip(startIndex).Take(pageSize).ToList()
                                            : queryReceipt.ToList();
            List<ReceiptModel> receiptList = new List<ReceiptModel>();

            foreach (Library_Receipt lr in receipts)
            {
                ReceiptModel data = new ReceiptModel();
                data.ID = lr.Id;
                data.SkippingID = lr.Library_Skipping.Id;
                data.EmployerID = lr.Library_Employee.Id;
                data.Money = lr.Money;
                data.Reason = lr.Reason;
                data.ReceiptDate = (DateTime)lr.ReceiptDate;
                data.CheckAgreed = (Boolean)lr.CheckAgreed;
                receiptList.Add(data);
            }

            _context.Dispose();
            return receiptList;
        }

        public ReceiptModel AddNewData(ReceiptModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Receipt receipts = new Library_Receipt();
            //Assign Para
            receipts.SkippingId = data.SkippingID;
            receipts.EmployerID = data.EmployerID;
            receipts.Money = data.Money;
            receipts.Reason = data.Reason;
            receipts.ReceiptDate = (DateTime)data.ReceiptDate;
            receipts.CheckAgreed = (bool)data.CheckAgreed ? true : false;
            //Insert
            _context.Library_Receipts.InsertOnSubmit(receipts);
            _context.SubmitChanges();
            //Assign new ID to Model
            data.ID = receipts.Id;
            _context.Dispose();
            return data;
        }

        public bool UpdateData(ReceiptModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Receipt receipt = _context.Library_Receipts.Where(lr => lr.Id == data.ID).FirstOrDefault();
            //Assign Para
            receipt.SkippingId = data.SkippingID;
            receipt.Money = data.Money;
            receipt.Reason = data.Reason;
            receipt.ReceiptDate = (DateTime)data.ReceiptDate;
            receipt.CheckAgreed = (bool)data.CheckAgreed ? true : false;
            //Update
            _context.SubmitChanges();
            _context.Dispose();
            return true;
        }

        public bool DeleteData(int Id)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Receipt receipt = _context.Library_Receipts.Where(lr => lr.Id == Id).FirstOrDefault();

            //Delete
            _context.Library_Receipts.DeleteOnSubmit(receipt);
            _context.SubmitChanges();
            //
            _context.Dispose();
            return true;
        }
    }
}